package com.example.mymarket.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.mymarket.Data_model.Banner;
import com.example.mymarket.Data_model.ProductDetail;
import com.example.mymarket.Data_model.ResponseData;
import com.example.mymarket.Data_model.info;
import com.example.mymarket.Fragment.HomeFragment;
import com.example.mymarket.R;
import com.example.mymarket.adapter.ImageAdapter;
import com.example.mymarket.api.ApiService;
import com.example.mymarket.interfaces.ResponseCallback;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.List;

public class ProductDetailActivity extends AppCompatActivity {

    private TextView tvProductName;
    private TextView tvProductPrice;
    private TextView tvProductDesc;
    private ImageView back_btn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail);

        // 绑定视图控件
        tvProductName = findViewById(R.id.tv_product_name);
        tvProductPrice = findViewById(R.id.tv_product_price);
        tvProductDesc = findViewById(R.id.tv_product_desc);
        back_btn = findViewById(R.id.iv_back);
        RecyclerView recyclerView = findViewById(R.id.rv_product_images);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        com.youth.banner.Banner ivProductImage = findViewById(R.id.product_detail_banner);


        int productId = getIntent().getIntExtra("productId", -1);
        if (productId == -1) {
            Toast.makeText(this, "商品ID无效", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }


        back_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        ApiService.getProductDetail(productId , new ResponseCallback<ResponseData<ProductDetail>>() {
            @Override
            public void onSuccess(ResponseData<ProductDetail> result) {
                info info = result.getData().getInfo();
                tvProductName.setText(info.getName());
                tvProductPrice.setText(info.getCounterPrice().toString());
                tvProductDesc.setText(info.getBrief());
                List<String> imgList = info.getGallery();
                ImageAdapter adapter = new ImageAdapter(ProductDetailActivity.this, imgList);
                recyclerView.setAdapter(adapter);

                ivProductImage.setAdapter(new BannerImageAdapter<String>(imgList) {


                    @Override
                    public void onBindView(BannerImageHolder holder, String data, int position, int size) {
                        //图片加载自己实现
                        Glide.with(holder.itemView)
                                .load(data)
                                .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                                .into(holder.imageView);
                    }
                }).addBannerLifecycleObserver(ProductDetailActivity.this).setIndicator(new CircleIndicator(ivProductImage.getContext()));

            }
        });

    }
}